// packageE/groupCode/groupCodeLabel/groupCodeLabel.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showTwo: false,
    shareBox: false,
    isLoadingEnd:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.id) {
      this.setData({
        id: options.id,
      });
    }
    if (options.scene) {
      let scene = decodeURIComponent(options.scene);
      if (scene) {
        var info_arr = [];
        info_arr = scene.split(",");
        for (let i = 0; i < info_arr.length; i++) {
          let chil_arr = [];
          chil_arr = info_arr[i].split("=");
          if (chil_arr[0] == "mid") {
            app._setMid(chil_arr[1]);
          }
          if (chil_arr[0] == "id") {
            this.setData({
              id: chil_arr[1],
            });
          }
        }
      }
    }
    this.getData();
  },
  goDetail(e) {
    let item = e.currentTarget.dataset.info;
    console.log(item);
    wx.navigateTo({
      url:
        "/packageE/groupCode/groupCodeShow/groupCodeShow?id=" +
        this.data.id +
        "&label=" +
        item,
    });
  },
  tapShow() {
    this.setData({
      showTwo: true,
    });
  },
  CloseBtn() {
    this.setData({
      showTwo: false,
      shareBox: false,
    });
  },
  shareBox(e) {
    wx.showLoading({
      title: "加载中",
    });
    var that = this;
    let urlStr = app.getNetAddresss(
      "plugin.group-code.api.group-code.get-share-poster"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        id: this.data.id,
        ingress: "weChatApplet",
      },
      success: (resdata) => {
        var res = resdata.data;
        wx.hideLoading({
          success: (res) => {},
        });
        if (res.result == 1) {
          console.log(res);
          that.setData({
            minImga: res.data.image_url,
            base: res.data.base64Image,
            shareBox: true,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
          console.log(res.msg);
        }
      },
    });
  },
  saveImg() {
    wx.getSetting({
      success(res) {
        if (!res.authSetting["scope.writePhotosAlbum"]) {
          wx.authorize({
            scope: "scope.writePhotosAlbum",
            success() {
              console.log("授权成功");
            },
          });
        }
      },
    });
    var imgSrc = this.data.minImga;
    wx.downloadFile({
      url: imgSrc,
      success: function (res) {
        console.log(res);
        //图片保存到本地
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success: function (data) {
            wx.showToast({
              title: "保存成功",
              icon: "none",
              duration: 2000,
            });
          },
          fail: function (err) {
            console.log(err);
            if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
              console.log("当初用户拒绝，再次发起授权");
              wx.openSetting({
                success(settingdata) {
                  if (settingdata.authSetting["scope.writePhotosAlbum"]) {
                    wx.showToast({
                      icon: "none",
                      title: "获取权限成功,再次长按保存图片",
                      duration: 1500,
                    });
                  } else {
                    wx.showToast({
                      icon: "none",
                      title: "获取权限失败,保存图片需要用户授权",
                      duration: 1500,
                    });
                  }
                },
              });
            }
          },
          complete(res) {
            console.log(res);
          },
        });
      },
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: this.data.info.code_name,
      path:
        "/packageE/groupCode/groupCodeLabel/groupCodeLabel?id=" + this.data.id,
    };
  },
  getData() {
    wx.showLoading({ title: '加载中',});
    var that = this;
    let urlStr = app.getNetAddresss(
      "plugin.group-code.api.group-code.get-group-code"
    );
    app._postNetWork({
      url: urlStr,
      data: {
        id: this.data.id,
      },
      success: (resdata) => {
        wx.hideLoading();
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);

          if (app._isTextEmpty(res.data.data)) {
            wx.redirectTo({
              url:
                "/packageE/groupCode/groupCodeShow/groupCodeShow?id=" +
                that.data.id,
            });
          } else {
            wx.setNavigationBarTitle({
              title: res.data.data.code_name,
            });
            this.setData({
              isLoadingEnd:true,
              info: res.data.data,
              shop_log: res.data.shop_log,
            });
          }
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
          console.log(res.msg);
          setTimeout(()=>wx.navigateBack(),1000)
        }
      },
    });
  },
});
